package day01;

import java.util.Arrays;

/**
 * @author Jetwang
 * @version 1.0
 * @since 2023/2/28 11:03
 */
public class FibonacciPlus {
    public static void main(String[] args) {
        for (int i = 0; i < 14; i++) {
            System.out.println(fibonacciPlus(i));
        }
    }

    /**
     * 空间换取时间
     * @param n
     * @return
     */
    public static int fibonacciPlus(int n) {
        int[] cache = new int[n+1];
        Arrays.fill(cache, -1);
        cache[0] = 0;
        if (n > 0) {
            cache[1] = 1;
        }
        return fibonacci(n, cache);
    }


    public static int fibonacci(int n, int[] cache) {
        if (cache[n] != -1) {
            return cache[n];
        }
        cache[n] =  fibonacci(n-1, cache) + fibonacci(n-2, cache);
        return cache[n];
    }
}
